var HttpStatusInterceptor = function ($q,$injector,$rootScope,$state,responseService) {
    return {
        responseError:function (response) {
            if (response.status == 401) {
                var  state = $injector.get('$rootScope').$state.current.name;
                $rootScope.stateBeforLogin = state;
                $rootScope.$state.go("login");
                return $q.reject(response);
            } else if (response.status === 404) {
                $rootScope.$state.go("404");
                return $q.reject(response);
            }
        },
        response:function (response) {
            if(response.status == 200 && response.data  && ((response.data.message && response.data.message.code ==200) || response.data.success ==true)){
                return response || $q.when(response);
            }else if(response.status == 401 ||(response.status == 200 && response.data  && (response.data.message && response.data.message.code ==400))){
                var  state = $state.current.name;
                $rootScope.stateBeforLogin = state;
                $state.go("login");
            }else {
                return $q.reject(response);
            }
        }
    }
};
HttpStatusInterceptor.$inject=["$q","$injector","$rootScope","$state","ResponseService"];

export default HttpStatusInterceptor;